#!/usr/bin/perl
#
# Build the language dirs for the internationalized counter
#
use strict;

use vars qw(@languages $orig $dest @subdirs %notdirs %notfiles $dir);
use vars qw($dirsmade $filesmade);


die "This isn't how we do things any more!\n";

@languages = qw(en no es fi nl);

die "No counter root"
    if !defined($ENV{COUNTER_ROOT});

$orig = $ENV{COUNTER_ROOT};
$dest = "$ENV{COUNTER_ROOT}/i18n";
@subdirs = qw(www cgi-bin);
%notdirs = map {$_ => 1} ("$dest/www", "$dest/cgi-bin", "$dest/phpinc",
			  "$orig/www/bycountry", "$orig/www/babylon");
%notfiles = map {$_ => 1} qw(. .. CVS);

for $dir (@subdirs) {
    my $lang;
    for $lang (@languages) {
	mirror("$orig/$dir", "$dest/$dir/$lang");
    }
}
print "$dirsmade dirs made\n";
print "$filesmade file links made\n";

makebasedir();
defaultenglish();

sub mirror {
    my $dir = shift;
    my $destdir = shift;
    
    warn "Mirror $dir to $destdir\n";
    if (! -d $destdir) {
	mkdir($destdir, 0777) || die "Unable to make $destdir\n";
	++ $dirsmade;
    }
    my @files = <$dir/*>;
    my $file;
    for $file (@files) {
	my $justfile = $file;
	$justfile =~ s/.*\///; # up to last slash
	next if $file =~ /~$/;
	next if $notfiles{$justfile};
	next if $notdirs{$file};
	my $tofile = "$destdir/$justfile";
	if (-l $file) {
	    warn "Ignoring symlink $file\n";
	} elsif (-d $file) {
	    mirror($file, $tofile);
	} elsif (-f $file) {
	    if (! ( -f $tofile || -l $tofile)) {
		symlink($file, $tofile) || die "Unable to symlink $tofile\n";
		++ $filesmade;
	    }
	}
    }

    my @files = <$dir/.*>;
    my $file;
    for $file (@files) {
        my $justfile = $file;
        $justfile =~ s/.*\///; # up to last slash
        next if $file =~ /~$/;
        next if $notfiles{$justfile};
        next if $notdirs{$file};
        my $tofile = "$destdir/$justfile";
        if (-l $file) {
            warn "Ignoring symlink $file\n";
        } elsif (-d $file) {
            mirror($file, $tofile);
        } elsif (-f $file) {
            if (! ( -f $tofile || -l $tofile)) {
                symlink($file, $tofile) || die "Unable to symlink $tofile\n";
                ++ $filesmade;
            }
        }
    }
}

# Make a base Web directory that uses language negotiation
# to serve the variants.
# Only links in the things that are not symlinks
sub makebasedir {
    open(FILES, "find $dest/www -type f|") || die "Unable to find";
    while (<FILES>) {
	chop;
	if (/\/www\/(\w\w)\//) {
	    next if /~$/;
	    next if /CVS/;
	    my $lang = $1;
	    my $tofile = $_;
	    $tofile =~ s/\/$lang\//\//; # take out lang
	    $tofile .= ".$lang";
	    warn "Using file $_ as $tofile\n";
	    symlink($_, $tofile);
	} else {
	    warn "Not interesting: $_\n";
	}
    }
}

sub defaultenglish {
# make sure English versions exist in the non-lang-specific dirs
    my $dir;
    for $dir (@subdirs) {
	open(FILES, "find $dest/$dir/en -type f -or -type l|");
	while (<FILES>) {
	    chop;
	    my $tofile = $_;
	    next if /~$/;
	    next if /.gz$/;
	    next if /CVS/;
	    $tofile =~ s/\/en\//\//;
	    if ($dir eq "www" && $tofile !~ /\.(css|gif)/) {
		$tofile .= ".en";
	    }
	    my $todir = $tofile;
	    $todir =~ s/\/[^\/]+$//;
	    if (! -d $todir) {
		mkdir($todir, 0777) || die "Unable to make $todir: $!";
	    }
	    if (! -f $tofile) {
		warn "Link $_ $tofile\n";
		symlink($_, $tofile) || die "link $tofile failed: $!";
	    }
	}
    }
}
